<?php
header( 'Content-Type:application/json; charset=utf-8' );
session_start();
if ( empty( $_SESSION[ 'userinfo' ] ) ) {
    $result = [
        'code' => '403',
        'msg' => '未登录，请登录',
    ];
    echo json_encode( $result );
    exit ;
}
$input = file_get_contents( 'php://input' );
$arr = json_decode( $input, true );
$tch_id = $arr[ 'tch_id' ] ?? '';
$cou_id = $arr[ 'cou_id' ] ?? '';

$dbms = 'mysql';
$host = '127.0.0.1';
$dbName = 'g09short';
$user = 'root';
$pass = 'G09short.';
$dsn = "$dbms:host=$host;dbname=$dbName";

date_default_timezone_set( 'PRC' );

try {
    $dbh = new PDO( $dsn, $user, $pass );
    // 查询课程班级人数作为作业应提交人数
    $sql = "SELECT * FROM zyh_cou_stu WHERE  cou_id = $cou_id ;";
    $stuCount = $dbh->query( $sql )->rowCount();

    $sql = "SELECT * FROM zyh_work WHERE tch_id = '".$tch_id ."' AND cou_id = $cou_id ;";
    $stmt = $dbh->query( $sql );
    $arr = $stmt->fetchAll( PDO::FETCH_ASSOC );
    foreach ( $arr as $key => $row ) {
        if ( date( 'Y-m-d H:i:s' )<$row[ 'work_startime' ] ) {
            $arr[ $key ][ 'work_state' ] = '未发布';
        } elseif ( date( 'Y-m-d H:i:s' )>$row[ 'work_endtime' ] ) {
            $arr[ $key ][ 'work_state' ] = '已结束';
        } else {
            $arr[ $key ][ 'work_state' ] = '进行中';
        }
        $sql = "SELECT * FROM zyh_submit WHERE work_id = '".$row[ 'work_id' ] ."'";
        $subCount = $dbh->query( $sql )->rowCount();
        $arr[ $key ][ 'work_upnumber' ] =$subCount . '/' . $stuCount;
    }
    //获取记录
    $result = [
        'code' => '200',
        'msg' => '查询成功！',
        'data' => $arr
    ];
    $dbh = null;
    echo json_encode( $result );
} catch ( PDOException $e ) {
    $result = [
        'code' => '500',
        'msg' => '$e->getMessage()',
    ];
    die( "$result" );
}
?>